package com.thirdsdk.qiniu;

import com.curry.android.util.StringUtil;

/**
 * 七牛帮助类
 * Created by wgf on 2016/1/27.
 *
 * @author wgf
 */
public class QiniuHelper {

    /**
     * 七牛图片尺寸
     */
    public enum QiniuImageSize {
        /** 原图 */
        IMAGE_SIZE_ORIGINAL,
        /** 七牛大图PNG，width=960px */
        IMAGE_SIZE_LARGE_PNG,
        /** 七牛大图JPG，width=960px */
        IMAGE_SIZE_LARGE_JPG,
        /** 七牛中图PNG，width=640px */
        IMAGE_SIZE_MIDDLE_PNG,
        /** 七牛中图JPG，width=640px */
        IMAGE_SIZE_MIDDLE_JPG,
        /** 七牛小图PNG，width=320px */
        IMAGE_SIZE_SMALL_PNG,
        /** 七牛小图JPG，width=320px */
        IMAGE_SIZE_SMALL_JPG,
        /** 七牛超小图PNG，width=100px，一般作列表封面 */
        IMAGE_SIZE_EXTRA_SMALL_PNG,
        /** 七牛超小图JPG，width=100px，一般作列表封面 */
        IMAGE_SIZE_EXTRA_SMALL_JPG,
        /** 七牛正方形超小图PNG，width=100px，一般作列表封面 */
        IMAGE_SIZE_EXTRA_SMALL_SQUARE_PNG,
        /** 七牛正方形超小图JPG，width=100px，一般作列表封面 */
        IMAGE_SIZE_EXTRA_SMALL_SQUARE_JPG,
        /** 七牛矩形超小图JPG，width=100px，一般作列表封面 */
        IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG
    }

    /**
     * 获取带七牛图片尺寸后缀的URL
     *
     * @param url       七牛图片地址
     * @param imageSize 七牛图片尺寸，如果为空，则使用原始尺寸图片
     * @return 带七牛图片尺寸后缀的URL
     */
    public static String getUrlWithQiniuImageSize(String url, QiniuImageSize imageSize) {
        if (StringUtil.isBlank(url)) {
            return url;
        }
        String urlWithImageSize = new StringBuilder().append(url)
                .append(getImageSizeText(imageSize)).toString();
        return urlWithImageSize;
    }

    /**
     * 获取URL，七牛正方形超小图PNG，width=100px
     *
     * @param url
     * @return
     */
    public static String getUrlWithQiniuListLogo(String url) {
        return getUrlWithQiniuImageSize(url, QiniuImageSize.IMAGE_SIZE_EXTRA_SMALL_SQUARE_PNG);
    }

    public static String getUrlWithMediumQiniuImage(String url) {
        return getUrlWithQiniuImageSize(url, QiniuImageSize.IMAGE_SIZE_MIDDLE_PNG);
    }

    public static String getUrlWithSmallQiniuImage(String url) {
        return getUrlWithQiniuImageSize(url, QiniuImageSize.IMAGE_SIZE_SMALL_PNG);
    }

    public static String getUrlWithExtraSmallQiniuImage(String url) {
        return getUrlWithQiniuImageSize(url, QiniuImageSize.IMAGE_SIZE_EXTRA_SMALL_PNG);
    }

    /**
     * 根据七牛图片类型，返回图片类型字符串
     *
     * @param imageSize 图片类型
     * @return 七牛图片大小参数
     */
    private static String getImageSizeText(QiniuImageSize imageSize) {
        if (imageSize == null) {
            imageSize = QiniuImageSize.IMAGE_SIZE_ORIGINAL;
        }
        String imageSizeText;
        switch (imageSize) {
            case IMAGE_SIZE_LARGE_PNG:
                imageSizeText = "-lg.png";
                break;
            case IMAGE_SIZE_LARGE_JPG:
                imageSizeText = "-lg.jpg";
                break;
            case IMAGE_SIZE_MIDDLE_PNG:
                imageSizeText = "-md.png";
                break;
            case IMAGE_SIZE_MIDDLE_JPG:
                imageSizeText = "-md.jpg";
                break;
            case IMAGE_SIZE_SMALL_PNG:
                imageSizeText = "-sm.png";
                break;
            case IMAGE_SIZE_SMALL_JPG:
                imageSizeText = "-sm.jpg";
                break;
            case IMAGE_SIZE_EXTRA_SMALL_PNG:
                imageSizeText = "-xs.png";
                break;
            case IMAGE_SIZE_EXTRA_SMALL_JPG:
                imageSizeText = "-xs.jpg";
                break;
            case IMAGE_SIZE_EXTRA_SMALL_SQUARE_PNG:
                imageSizeText = "-xssq.png";
                break;
            case IMAGE_SIZE_EXTRA_SMALL_SQUARE_JPG:
                imageSizeText = "-xssq.jpg";
                break;
            case IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG:
                imageSizeText = "-storelogo.jpg";
                break;
            case IMAGE_SIZE_ORIGINAL:
                imageSizeText = "";
                break;
            default:
                imageSizeText = "";
                break;
        }
        return imageSizeText;
    }

}
